package com.kh.mychart.use;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.SparseArray;
import android.widget.SeekBar;

import com.kh.mychart.ChartLineBarAdapter;
import com.kh.mychart.LineBarChart;
import com.kh.mychart.MonthValue;
import com.lovely3x.view.HeightView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    StatisticsView statisticsView;
    LineBarChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //-----------------------
        RecyclerView recyclerview = findViewById(R.id.recyclerview);
//        AppCompatSeekBar sk = findViewById(R.id.sk);
//        chart = findViewById(R.id.chart);

//        sk.setOnSeekBarChangeListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerview.setLayoutManager(layoutManager);
        List<SparseArray<MonthValue>> list = new ArrayList<>();
        Integer arr[] = new Integer[]{685975, 399483, 718081, 715863, 628703, 574041, 717756, 693439, 717669, 766218, 835152, 873877};
        Integer last[] = new Integer[]{422171, 422496, 511538, 461198, 449552, 452761, 512026, 542470, 580009, 625121, 625950, 688088};
        List<Integer> arrList = new ArrayList<>();
        for (Integer value : arr) {
            //筛选过滤
            arrList.add(value);
        }

        List<Integer> lastList = new ArrayList<>();
        for (Integer value : last) {
            lastList.add(value);
        }

        List<MonthValue> currentMvList = handleData(arrList, 2019);
        int currentMvListSize = currentMvList.size();
        List<MonthValue> lastMvList = handleData(lastList, 2018);
        int lastMvListSize = lastMvList.size();
        HeightView hv_y = findViewById(R.id.hv_y);

        List<Integer> all = new ArrayList<>();
        all.addAll(arrList);
        all.addAll(lastList);

        hv_y.setLines((Collections.max(all) / 100 + 1) * 100 / 5);
        for (int i = 0; i < 12; i++) {
//            MonthValue it = new MonthValue();
//            MonthValue.Point data = new MonthValue.Point();
//            data.labelText = String.format("%02d月", i + 1);
//            data.y = arr[i] * 2 / 10000;
//            data.real = arr[i];
//            data.markText = String.format("%d(万)", (int) arr[i] / 10000);
//            it.current = data;
            SparseArray<MonthValue> item = new SparseArray<>();
            if (i < currentMvListSize) item.put(1, currentMvList.get(i));

//            MonthValue monthValueLast = new MonthValue();
//            MonthValue.Point data2 = new MonthValue.Point();
//            data2.labelText = String.format("%02d月", i + 1);
//            data2.y = last[i] * 2 / 10000;
//            data2.real = last[i];
//            data2.markText = String.format("%d(万)", (int) last[i] / 10000);
//            monthValueLast.current = data2;
            if (i < lastMvListSize) item.put(2, lastMvList.get(i));
            list.add(item);
        }

        ChartLineBarAdapter adapter = ChartLineBarAdapter.create(list, findViewById(android.R.id.content));
        recyclerview.setAdapter(adapter);

        //---------------------
//        statisticsView = findViewById(R.id.statisticsView);
//        statisticsView.setBottomStr(new String[]{"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"});
//        statisticsView.setValues(new float[]{10f, 90f, 33f, 66f, 42f, 99f, 0f});
    }

    public List<MonthValue> handleData(List<Integer> list, int year) {
        int size = list.size();
        List<MonthValue> listMV = new ArrayList<>();
        String format = "%d.%02d";
        for (int position = 0; position < size; position++) {
            MonthValue mv = new MonthValue();
            MonthValue.Point next = new MonthValue.Point();
            int nextPosition = size - 1 == position ? position : position + 1;
            next.labelText = String.format(format, year, nextPosition + 1);
            next.y = list.get(nextPosition) * 2 / 10000;
            next.real = list.get(nextPosition);
            next.markText = String.format("%d(万)", (int) list.get(nextPosition) / 10000);
            //
            MonthValue.Point pre = new MonthValue.Point();
            int prePosition = position == 0 ? 0 : position - 1;
            pre.labelText = String.format(format, year, prePosition + 1);
            pre.y = list.get(prePosition) * 2 / 10000;
            pre.real = list.get(prePosition);
            pre.markText = String.format("%d(万)", (int) list.get(prePosition) / 10000);
            //current
            MonthValue.Point current = new MonthValue.Point();
            current.labelText = String.format(format, year, position + 1);
            current.y = list.get(position) * 2 / 10000;
            current.real = list.get(position);
            current.markText = String.format("%d(万)", (int) list.get(position) / 10000);
            if (position == 0) {
                if (size > 1) mv.next = next;
            } else if (size - 1 == position) {
                mv.pre = pre;
            } else {
                mv.next = next;
                mv.pre = pre;
            }
            mv.current = current;
            listMV.add(mv);
        }
        return listMV;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//        chart.barWidth = progress;
//        chart.invalidate();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
